package com.globalblue.view;

import java.awt.Desktop;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import org.apache.http.client.utils.URIBuilder;

import com.globalblue.ConfigManager;
import com.globalblue.Configurations;

public class LoadUrlDialog {

	private JFrame frame;
	private JTextField username;
	private JPasswordField password;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					LoadUrlDialog window = new LoadUrlDialog();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public LoadUrlDialog() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		ConfigManager manager = new ConfigManager();
		manager.readConfig();
		frame = new JFrame();
		frame.setBounds(100, 100, 820, 575);
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		frame.getContentPane().setLayout(null);

		JPanel panel = new JPanel();
		panel.setBounds(10, 11, 784, 515);
		frame.getContentPane().add(panel);
		panel.setLayout(null);

		JLabel lblNewLabel = new JLabel("Url To Load");
		lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblNewLabel.setBounds(166, 125, 147, 14);
		panel.add(lblNewLabel);

		final JComboBox<String> urlCombo = new JComboBox<String>();
		urlCombo.setEditable(true);
		urlCombo.setBounds(390, 124, 214, 20);
		urlCombo.setModel(new DefaultComboBoxModel<String>(Configurations.getUrlToLoad()));
		urlCombo.addItem("");
		urlCombo.setSelectedItem("");

		panel.add(urlCombo);

		JLabel lblNewLabel_1 = new JLabel("Username");
		lblNewLabel_1.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblNewLabel_1.setBounds(166, 160, 147, 14);
		panel.add(lblNewLabel_1);

		username = new JTextField();
		username.setBounds(390, 159, 214, 20);
		panel.add(username);
		username.setColumns(10);

		JLabel lblNewLabel_2 = new JLabel("Password");
		lblNewLabel_2.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblNewLabel_2.setBounds(166, 191, 147, 14);
		panel.add(lblNewLabel_2);

		password = new JPasswordField();
		password.setBounds(390, 190, 214, 20);
		panel.add(password);

		JButton btnLoadUrl = new JButton("Load Web Page");

		btnLoadUrl.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					String urlToLoad = urlCombo.getSelectedItem().toString();
					ConfigManager.addUrlToLoad(urlToLoad);
					URIBuilder uriBuilder = new URIBuilder(urlToLoad);
					uriBuilder.setUserInfo("uwijekoon", "usw#8486@GB");

					if (Desktop.isDesktopSupported()) {
						Desktop desktop = Desktop.getDesktop();
						desktop.browse(uriBuilder.build());
					} else {
						Runtime runtime = Runtime.getRuntime();
						runtime.exec("xdg-open " + uriBuilder.build());
					}
					
					frame.dispose();
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}
		});
		btnLoadUrl.setFont(new Font("Tahoma", Font.PLAIN, 14));
		btnLoadUrl.setBounds(300, 235, 147, 23);
		panel.add(btnLoadUrl);
		
		JLabel lblEnterTheDetails = new JLabel("Enter the details to verify deployment");
		lblEnterTheDetails.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblEnterTheDetails.setBounds(238, 39, 277, 23);
		panel.add(lblEnterTheDetails);
	}
	
	public void showLoadUrlWindow(){
		frame.setVisible(true);
	}
}
